package org.kernely.test.log;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;

import org.kernely.test.log.LogView.LogButton;
import org.kernely.test.testbundle.TestBundle;
import org.kernely.ui.mvp.KernelyPresenter;
import org.osgi.service.log.LogEntry;
import org.osgi.service.log.LogService;

import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickListener;

/**
 *	Presenter for the log tests.
 */
public class LogPresenter extends KernelyPresenter<LogView> {
	@Override
	/**
	 * @see org.kernely.core.service.IKernelyPresenter#bind()
	 */
	public final void bind() {
		// Adding all the buttons' listeners
		this.view.getCheckInfo().addListener(new CheckListener());
		this.view.getCheckWarning().addListener(new CheckListener());
		this.view.getCheckDebug().addListener(new CheckListener());
		this.view.getCheckError().addListener(new CheckListener());


		this.view.getAddInfo().addListener(new AddListener());
		this.view.getAddWarning().addListener(new AddListener());
		this.view.getAddDebug().addListener(new AddListener());
		this.view.getAddError().addListener(new AddListener());
		this.view.getRefreshButton().addListener(new ClickListener(){
			private static final long serialVersionUID = 7871219904239488944L;

			public void buttonClick (Button.ClickEvent event) {
				listLog();
			}
		});

		listLog();

	}
	
	/**
	 * Implementation of the ValueChangeListener to define our CheckBox listener
	 */
	private class CheckListener implements ValueChangeListener{

		/**
		 * The Serial Version ID
		 */
		private static final long serialVersionUID = 1L;

		@Override
		/**
		 * Called when the box value change
		 */
		public void valueChange(ValueChangeEvent event) {
			listLog();
		}

	}

	/**
	 * Implementation of ClickListener to define our add button listener
	 */
	private class AddListener implements ClickListener{
		/**
		 * The Serial Version ID
		 */
		private static final long serialVersionUID = 51651231312182L;

		/**
		 * Called when a ClickEvent is fired
		 */
		public void buttonClick (Button.ClickEvent event) {
			if (view.getTestLog().toString() != null && ! view.getTestLog().toString().equals("") && !view.getTestLog().toString().equals("null")){
				// Retrieve the button which fired this event
				LogButton source = (LogButton) event.getButton();
				
				TestBundle.getLog().log(source.getLogLevel(), view.getTestLog().toString());
				listLog();
			}
		}
	}

	private void listLog(){
		// Retrieve the CheckBoxes states
		boolean info = ((Boolean) this.view.getCheckInfo().getValue()).booleanValue();
		boolean warning = ((Boolean) this.view.getCheckWarning().getValue()).booleanValue();
		boolean debug = ((Boolean) this.view.getAddDebug().getValue()).booleanValue();
		boolean error = ((Boolean) this.view.getAddError().getValue()).booleanValue();
		// Clean the table
		this.view.getLogTable().removeAllItems();
		// Retrieve all registered logs into the Apache Log Service
		Enumeration<LogEntry> latestLogs = TestBundle.getLogReader().getLog();

		int i = 1;
		boolean display = false;
		while(latestLogs.hasMoreElements()){
			LogEntry e = latestLogs.nextElement();
			// Getting the log level : INFO, DEBUG, WARNING, ERROR
			String level;
			switch(e.getLevel()){
			case LogService.LOG_INFO:
				level = "INFO";
				display = info;
				break;
			case LogService.LOG_WARNING :
				level = "WARNING";
				display = warning;
				break;
			case LogService.LOG_DEBUG:
				level = "DEBUG";
				display = debug;
				break;
			case LogService.LOG_ERROR:
				level = "ERROR";
				display = error;
				break;
			default :
				level = "UNKNOWN";
			}
			if (display){
				// Format the date : Month - Day - Year
				Date d = new Date(e.getTime());
				DateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy - HH:mm:ss");
				// Add the item to the table
				this.view.getLogTable().addItem(new Object[] {dateFormat.format(d), level, e.getBundle().getSymbolicName(), e.getMessage()}, new Integer(i));
				i++;
			}
		}

	}
}
